import store from '@/store';
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
// 解决重复点击同一个路由时报错的问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => {})
};

Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'home',
    component: Home
  },
  {
    path: '/goods',
    name: 'goods',
    component: () => import( /* webpackChunkName: "about" */ '../views/Goods.vue')
  },
  {
    path: '/user',
    name: 'user',
    redirect: "/user/cart",
    component: () => import( /* webpackChunkName: "about" */ '../views/User.vue'),
    children: [{
      path: "cart",
      component: () => import( /* webpackChunkName: "about" */ '../components/user/Cart.vue'),

    }]
    // 路由独享守卫
    // beforeEnter:((to,from,next)=>{
    //       let token = localStorage.getItem("x-auth-token")
    //       if(token){
    //         next()
    //       }else{
    //         store.dispatch("toastStatus/asyncIsShowToast",{
    //           isShowToast:true,
    //           toastMsg:"你还没有登录",
    //           toastType:"warning",
    //         })

    //       }
    //   })

  },
  {
    path: '/order',
    name: 'order',
    component: () => import( /* webpackChunkName: "about" */ '../views/OrderView.vue')
  },
  {
    path: '/free',
    name: 'free',
    component: () => import( /* webpackChunkName: "about" */ '../views/FreeView.vue')
  },
  {
    path: '/details',
    name: 'details',
    component: () => import( /* webpackChunkName: "about" */ '../views/Details.vue')
  },
  {
    path: '*',
    name: 'error',
    component: () => import( /* webpackChunkName: "about" */ '../views/Error.vue')
  },
]


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// router.beforeEach((to,from,next)=>{
//   if(to.path=="/user"){
//     let token = localStorage.getItem("x-auth-token")
//     if(token){
//       next()
//     }else{
//       store.dispatch("toastStatus/asyncIsShowToast",{
//         isShowToast:true,
//         toastMsg:"你还没有登录",
//         toastType:"warning",
//       })
//       return
//     }
//   }
//   next()
// })

export default router